//
//  SecondCollectionCell.m
//  BuffetNews
//
//  Created by weidawei on 14-10-30.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "SecondCollectionCell.h"
#import "UIImage+Tint.h"
#import "Myflow.h"
#import "theCustom.h"
#import "imageCollentionCell.h"
#import "iogoImageCell.h"
#import "otherCell.h"
#import "NetHandler.h"
#import "ArticlesNews.h"
#import "OtherCell2.h"
#import "OtherCell3.h"
#import "OtherCell4.h"
#import "OtherCell5.h"
#import "UIImageView+WebCache.h"
#import "CommentViewController.h"
#import "MyFlowTow.h"
#import "UIImageView+myImage.h"
@implementation SecondCollectionCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
       
//        self.articles = [NSMutableArray array];
//        self.dic = [NSDictionary dictionary];
//        self.dic1 = [NSDictionary dictionary];
//        self.dic2 = [NSDictionary dictionary];
//        self.articles1 = [NSMutableArray array];
//        self.articles2 = [NSMutableArray array];
//        self.articles3 = [NSMutableArray array];
//        self.articles4 = [NSMutableArray array];
//        self.dic3 = [NSDictionary dictionary];
//        self.dic4 = [NSDictionary dictionary];
//        self.dic5 = [NSDictionary dictionary];
//        self.dic6 = [NSDictionary dictionary];
//        self.urlImage = [NSMutableArray array];
        self.atcenes = [[ArticlesNews alloc] init];

        
        
        Myflow *flowLayout = [[Myflow alloc] init];
        
        self.collerction = [[UICollectionView alloc] initWithFrame:self.contentView.bounds collectionViewLayout:flowLayout];
        self.collerction.delegate = self;
        self.collerction.dataSource = self;
        self.collerction.backgroundColor = [UIColor whiteColor];
        
        [self.contentView addSubview:self.collerction];
        
        
        [_collerction release];
        
        [self.collerction registerClass:[imageCollentionCell class] forCellWithReuseIdentifier:@"2"];
        [self.collerction registerClass:[OtherCell2 class] forCellWithReuseIdentifier:@"3"];

        self.loghe  = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, self.contentView.frame.size.width, self.contentView.frame.size.height / 7 - 1)];
        
       
//        self.loghe.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:self.loghe];
        [_loghe release];

    }
    return self;
}

- (void)resetLayout
{
//    NSLog(@"=======%@", [self.atcenes.pkArrBig[1] objectForKey:@"media"]);
    
    if ([[[self.atcenes.pkArrBig firstObject] objectForKey:@"media"] count] == 0) {
        
        MyFlowTow * flow = [[MyFlowTow alloc] init];
        [self.collerction setCollectionViewLayout:flow animated:NO];
        
    } else {
         Myflow *flowLayout = [[Myflow alloc] init];
        [self.collerction setCollectionViewLayout:flowLayout animated:NO];
    }
}

// 数组判断
- (void)arrJudge
{
    NSLog(@"先进来的时候%d", [self.atcenes.pkArrBig count]);
    
//    if ([[self.atcenes.pkArrBig lastObject] count] != 6) {
//        [self.atcenes.pkArrBig removeLastObject];
//        NSLog(@"处理完的时候%d", [self.atcenes.pkArrBig count]);
//    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
//    NSLog(@"()()()()()()()()()()%@", self.atcenes.bgimage_url);
    [self.loghe sd_setImageWithURL:[NSURL URLWithString:self.atcenes.bgimage_url]];
    [self arrJudge];
//    [self.collerction reloadData];

   }




- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.atcenes.pkArrBig.count ;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    

    
    
    self.imageCollentionCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"2" forIndexPath:indexPath];
    
    if ([[[self.atcenes.pkArrBig firstObject] objectForKey:@"media"] count] == 0) {
        
        self.otherCell2 = [collectionView dequeueReusableCellWithReuseIdentifier:@"3" forIndexPath:indexPath];
        self.otherCell2.label1.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
        self.otherCell2.label2.font = [UIFont fontWithName:@"Helvetica-Bold" size:12];

        
       if (indexPath.row == 4) {
           [self.otherCell2.img sd_setImageWithURL:[NSURL URLWithString:[[[self.atcenes.pkArrBig[indexPath.item] objectForKey:@"media"]firstObject] objectForKey:@"m_url"]]placeholderImage:[UIImage imageNamed:@"加载...png"]];
//           [self.otherCell2.img setImage:[UIImage imageNamed:@"加载...png"]];
           if ([[self.atcenes.pkArrBig[indexPath.item] objectForKey:@"media"] count] == 0) {
               
               
               self.otherCell2.img.image = [UIImage imageNamed:@"加载...png"];
               
               [self.otherCell2.label2 setText: [self.atcenes.pkArrBig[indexPath.item ] objectForKey:@"title"]];
               
               
           }
           
           self.otherCell2.label2.textColor = [UIColor whiteColor];
           
//           self.otherCell2.img.image = nil;
           
           
           
           
           [self.otherCell2.label2 setText: [self.atcenes.pkArrBig[indexPath.item ] objectForKey:@"title"]];
           
           
           
           [self.otherCell2.label1 setText: @" "];
           
            
            
            return self.otherCell2;
            
            
            
       } else {
           [self.otherCell2.label1 setText: [self.atcenes.pkArrBig[indexPath.item ] objectForKey:@"title"]];
           
           self.otherCell2.label1.numberOfLines = 0;
           

           
           [self.otherCell2.label2 setText: [self.atcenes.pkArrBig[indexPath.item ] objectForKey:@"auther_name"]];
           
           [self.otherCell2.img sd_setImageWithURL:nil];
           

           self.otherCell2.label2.textColor = [UIColor grayColor];
           
           
           return self.otherCell2;
       }
        
            

        
        return self.otherCell2;
        
        

        
    } else {
        
 
        
        
  
            
    if (indexPath.row == 0) {
            
            self.imageCollentionCell.imageView.image = [UIImage imageNamed:@"加载...png"];
            
            
            
            if ([[self.atcenes.pkArrBig[indexPath.item] objectForKey:@"media"] count] != 0) {
                
                
                
                [self.imageCollentionCell.imageView sd_setImageWithURL:[NSURL URLWithString:[[[self.atcenes.pkArrBig[indexPath.item] objectForKey:@"media"]firstObject] objectForKey:@"m_url"]]placeholderImage:[UIImage imageNamed:@"加载...png"]];
                
                [self.imageCollentionCell.label1 setText: @" "];
                
                [self.imageCollentionCell.label2 setText: [self.atcenes.pkArrBig[indexPath.item ] objectForKey:@"title"]];
                
                self.imageCollentionCell.label2.font = [UIFont fontWithName:@"Helvetica-Bold" size:12];
                
                
                
                self.imageCollentionCell.label2.textColor = [UIColor whiteColor];
                
                
                
            }
            
            [self.imageCollentionCell.logoImage sd_setImageWithURL:nil];
            
            
            
            return self.imageCollentionCell;
            
            
            
        } else  {
            
            
            
            
            
            [self.imageCollentionCell.label1 setText: [self.atcenes.pkArrBig[indexPath.item ] objectForKey:@"title"]];
            
            self.imageCollentionCell.label1.numberOfLines = 0;
            
            self.imageCollentionCell.label1.font = [UIFont fontWithName:@"Helvetica-Bold" size:12];
            
            [self.imageCollentionCell.label2 setText: [self.atcenes.pkArrBig[indexPath.item ] objectForKey:@"auther_name"]];
            
            self.imageCollentionCell.label2.textColor = [UIColor grayColor];
            
            self.imageCollentionCell.label2.font = [UIFont fontWithName:@"Helvetica-Bold" size:12];
            
            [self.imageCollentionCell.imageView sd_setImageWithURL:nil];
            
            
            
            [self.imageCollentionCell.logoImage sd_setImageWithURL:nil];
            
            
            
            return self.imageCollentionCell;
            
        }
        
        
        
        
        
        
        
        
        
    }


    
    
    
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{


//        [self.target performSelector:self.action withObject:self];
        NSNotificationCenter * censt = [NSNotificationCenter defaultCenter];
        [censt postNotificationName:@"weburl" object:self.atcenes.pkArrBig[indexPath.item]];
    if (indexPath.row == 4) {
//        NSLog(@"2131231!!#######%d", [[self.atcenes.pkArrBig[indexPath.item] objectForKey:@"media"] count] );
    }
//    NSLog(@"点了么 = %@",
//          self.atcenes.weburlArr);
    
}


- (void)dealloc
{
    [_articles release];
    [_bgimage_url release];
    [_dic release];
    [_collerction release];
    [_pageControl release];
    [_apiString release];
    [_theCustom release];
    [_imageCollentionCell release];
    [_iogoImageCell release];
    [_otherCell2 release];
    [_atcenes release];
    [_loghe release];
    
    // 移除当前对象监听的事件
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
